package com.silas.quartz.base01;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class DemoRunnable implements Runnable {

	// 调度器工厂
	private SchedulerFactory sf = new StdSchedulerFactory();

	@Override
	public void run() {

		// 任务
		JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
				.withIdentity("job_1", "group_1").build();

		// 触发器
		Trigger trigger = TriggerBuilder
				.newTrigger()
				.withIdentity("trigger_1", "group_1")
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.withIntervalInSeconds(5).withRepeatCount(10))
				.build();

		// 调度器
		try {
			// 实例化调度器
			Scheduler scheduler = sf.getScheduler();
			// 将任务对象及触发器设置进调度器中
			scheduler.scheduleJob(jobDetail, trigger);
			// 启动调度器
			scheduler.start();

		} catch (SchedulerException e) {
			e.printStackTrace();
		}

	}

}
